package com.atguigu.juc02;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class Number //资源类
{
    private int num = 0;

    public int getNum() {
        return num;
    }

    public void setNum() {

        num++;
    }

    private AtomicInteger atomicInteger = new AtomicInteger();

    public AtomicInteger getAtomicInteger() {
        return atomicInteger;
    }

    public void setAtomicInteger() {
        atomicInteger.getAndIncrement();
    }
}


/**
 * @auther zzyy
 * @create 2021-08-05 21:03
 * <p>
 * int
 * Integer
 * AtomicInteger
 */
public class CASDemo2 {
    public static void main(String[] args) throws InterruptedException //main一切方法的入口
    {
        Number number = new Number();

        for (int i = 1; i <= 100; i++) {
            new Thread(() -> {
                for (int j = 1; j <= 400; j++) {
                    number.setNum();
                    number.setAtomicInteger();
                }
            }, "input thread name").start();
        }
//        TimeUnit.SECONDS.sleep(3);
        System.out.println("number.getNum() = " + number.getNum());
        System.out.println("number.getAtomicInteger() = " + number.getAtomicInteger());

//---------------------------------------------------------------
//        AtomicInteger atomicInteger = new AtomicInteger(1);
//
//        atomicInteger.compareAndSet(1, 2021);
//        atomicInteger.compareAndSet(2021, 2022);
//        System.out.println("atomicInteger.get() = " + atomicInteger.get());
//---------------------------------------------------------------


    }
}
